from typing import List

from fastapi import APIRouter, Request
from fastapi import Depends
from config.get_db import get_db
from module_admin.entity.vo.cms_order_approve_vo import OrderModelForApprove
from module_admin.entity.vo.cms_order_bill_vo import OrderBillModelForOperate, OrderBillModelForQuery, \
    OrderBillPageObject, OrderBillModelForOperateBatch, OrderBillModelForPage
from module_admin.entity.vo.cms_order_dispatch_vo import OrderDispatchModelForCreate, OrderDetails
from module_admin.entity.vo.cms_order_vo import OrderModel, OrderPageObject, OrderModelForQuery
from module_admin.service.cms_order_approve_service import CmsOrderApproveService
from module_admin.service.cms_order_bill_service import CmsOrderBillService
from module_admin.service.cms_order_dispatch_service import CmsOrderDispatchService
from module_admin.service.cms_order_service import CmsOrderService
from module_admin.dao.dept_dao import *
from module_admin.service.login_service import get_current_user_details
from utils.page_util import get_page_obj, get_new_page_obj
from utils.response_util import *
from utils.log_util import *

orderBillController = APIRouter(dependencies=[Depends(get_current_user_details)])


@orderBillController.post("/clear/opeate", summary="财务-运单结算-结算")
async def settlement(request: Request, order_bill_opeate: OrderBillModelForOperate,
                     query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsOrderBillService.settlement(query_db, order_bill_opeate)
        return response_200(data=dept_query_result, message="成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@orderBillController.post("/clear/opeateBatch", summary="财务-运单结算-结算批量")
async def settlement(request: Request, order_bill_opeate_list: List[int],
                     query_db: Session = Depends(get_db)):
    try:
        dept_query_result = CmsOrderBillService.settlement_list(query_db, order_bill_opeate_list)
        return response_200(data=dept_query_result, message="成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@orderBillController.post("/clear/page", response_model=OrderBillModelForPage, summary="财务-运单结算分页")
async def page_order(request: Request, order_bill_page_query: OrderBillPageObject, db: Session = Depends(get_db)):
    try:
        # query_object = OrderBillModelForQuery(**order_bill_page_query.dict())
        page_result = CmsOrderBillService.page(db, order_bill_page_query)
        total = CmsOrderBillService.count(db, order_bill_page_query)
        list_format_datetime(page_result)
        # 分页操作
        page_result_after = get_new_page_obj(page_result, order_bill_page_query.page_num,
                                             order_bill_page_query.page_size, total)
        # 处理
        return response_200(data=page_result_after, message="获得成功")
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
